package com.sun.controller;

import com.alibaba.fastjson.JSON;
import com.sun.entity.Book;
import com.sun.service.BookService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author SunRui
 * @description TODO
 * @date 2024/8/10 21:55
 */
@Controller
@RequestMapping("/BookController")
public class BookController{
    @Autowired
    private BookService bookService;
    private static Logger logger = Logger.getLogger(BookController.class);
    private String jsonString;
    private String str;

    @RequestMapping("/bookLoad")
    public void bookLoad(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String text = req.getParameter("text");
        int categoryId = Integer.parseInt(req.getParameter("categoryId"));
        //图书列表加载
        logger.debug("图书列表加载传输的数据为：" + text + ";" + categoryId);
        List<Book> bookList = bookService.getList(text, categoryId);
        System.out.println("bookList:" + bookList);
        jsonString = JSON.toJSONString(bookList);
        resp.getWriter().write(jsonString);
    }

    @RequestMapping("/bookDetail")
    public void bookDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //书籍详情
        int id = Integer.parseInt(req.getParameter("bookId"));
        Book book = bookService.getBook(id);
        logger.debug("获取的书籍信息:" + book.toString());
        jsonString = JSON.toJSONString(book);
        resp.getWriter().write(jsonString);
    }

    @RequestMapping("/addTS")
    public void addTS(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        //暂存架
        String code = req.getParameter("code");
        List<String> codeArray = (List<String>) session.getAttribute("codeArray");
        if (codeArray == null) {
            List<String> codes = new ArrayList<>();
            session.setAttribute("codeArray", codes);
        }
        codeArray = (List<String>) session.getAttribute("codeArray");
        if (codeArray.contains(code)) {
            logger.debug("已存在：" + code);
            str = "have";
        } else {
            codeArray.add(code);
            logger.debug("目前暂存架有：" + codeArray.toString());
            str = "ok";
        }
        jsonString = JSON.toJSONString(str);
        resp.getWriter().write(jsonString);
    }

    @RequestMapping("/borrowTsShow")
    public void borrowTsShow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取暂存架数据
        HttpSession session = req.getSession();
        List<String> codeArrays = (List<String>) session.getAttribute("codeArray");
        List<Book> bookTs = new ArrayList<>();
        if (codeArrays == null || codeArrays.isEmpty()) {
            // 暂存架没有数据，可以返回提示信息或者执行其他操作
            logger.debug("暂存架没有数据");
        } else {
            bookTs = new ArrayList<>();
            for (String codes : codeArrays) {
                Book books = bookService.bookTs(codes);
                bookTs.add(books);
            }
        }
        jsonString = JSON.toJSONString(bookTs);
        logger.debug("暂存架数据缓存为：" + jsonString);
        resp.getWriter().write(jsonString);
    }

    @RequestMapping("/eliminateBookTs")
    public void eliminateBookTs(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收传回要清除的暂存架图书id
        HttpSession session = req.getSession();
        String elBookTs = req.getParameter("bookIds");
        if (elBookTs != null && elBookTs.length() > 2) {
            elBookTs = elBookTs.substring(1, elBookTs.length() - 1);
            String[] eliminateBookTs = elBookTs.split(",");
            List<String> elBootTsId = new ArrayList<>();
            for (String bookId : eliminateBookTs) {
                elBootTsId.add(bookId.replace("\"", ""));
            }
            //获取缓存架数据进行清除操作
            List<String> eliminateCodeArray = (List<String>) session.getAttribute("codeArray");
            logger.debug("elBootTsId:" + elBootTsId.toString());
            logger.debug("eliminateCodeArray:" + eliminateCodeArray.toString());
            if (eliminateCodeArray != null) {
                for (String bookId : elBootTsId) {
                    if (eliminateCodeArray.contains(bookId)) {
                        eliminateCodeArray.remove(bookId);
                    }
                }
                session.setAttribute("codeArray", eliminateCodeArray);
                str = "ok";
            }
            logger.debug("清除结束(codeArray)：" + (List<String>) session.getAttribute("codeArray"));
        } else {
            //处理elBookTs为空或长度不足的情况
            logger.debug("elBookTs为空或长度不足");
        }
        jsonString = JSON.toJSONString(str);
        resp.getWriter().write(jsonString);
    }
}
